﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace ACRE.Tab_Classes
{
    class Data_Dump
    {
        public static void Dump_All(Label data_dir, Label dump_dir, ProgressBar pBar)
        {
            pBar.Value = 0;
            pBar.Maximum = RappelzDataCipher.rappelz_data.Count;
            foreach (RAPPELZDATA_INDEX rappelz_index in RappelzDataCipher.rappelz_data)
            {
                RappelzDataCipher.DumpFile(data_dir.Text, dump_dir.Text + "\\" + Path.GetExtension(rappelz_index.name).Replace(".", ""), rappelz_index.name, rappelz_index.offset, rappelz_index.DataID, rappelz_index.size);
                pBar.Value++;
            }
        }

        public static void Dump_File(Label data_dir, Label dump_dir, DataGridView dgv)
        {
            int n = dgv.CurrentCell.RowIndex;
            RappelzDataCipher.DumpFile(data_dir.Text, dump_dir.Text, dgv.Rows[n].Cells[0].Value.ToString(), (int)dgv.Rows[n].Cells[4].Value, (int)dgv.Rows[n].Cells[3].Value, (int)dgv.Rows[n].Cells[2].Value);
        }

        public static void Dump_Extension(Label data_dir, Label dump_dir, TextBox ext_box, ProgressBar pBar)
        {
            pBar.Value = 0;
            pBar.Maximum = RappelzDataCipher.rappelz_data.Count;
            foreach (RAPPELZDATA_INDEX rappelz_index in RappelzDataCipher.rappelz_data)
            {
                if (Path.GetExtension(rappelz_index.name).Replace(".", "") == ext_box.Text)
                {
                    RappelzDataCipher.DumpFile(data_dir.Text, dump_dir.Text, rappelz_index.name, rappelz_index.offset, rappelz_index.DataID, rappelz_index.size);
                    pBar.Value++;
                }
                pBar.Value++;
            }
        }

        public static void Read_Type(Label data_dir, Label file_count, ProgressBar pBar, DataGridView dgv)
        {
            if (OPT.GetString("db.load_flag") == "0")
            {
                try
                {
                    RappelzDataCipher.Open(data_dir.Text + "\\Data.000");
                    file_count.Text = "File Count: " + RappelzDataCipher.rappelz_data.Count.ToString();

                    dgv.RowCount = RappelzDataCipher.rappelz_data.Count + 1;
                    for (int i = 0; i < RappelzDataCipher.rappelz_data.Count; i++)
                    {
                        dgv.Rows[i].Cells[0].Value = RappelzDataCipher.rappelz_data[i].name;
                        dgv.Rows[i].Cells[1].Value = RappelzDataCipher.rappelz_data[i].hash;
                        dgv.Rows[i].Cells[2].Value = RappelzDataCipher.rappelz_data[i].size;
                        dgv.Rows[i].Cells[3].Value = RappelzDataCipher.rappelz_data[i].DataID;
                        dgv.Rows[i].Cells[4].Value = RappelzDataCipher.rappelz_data[i].offset;
                    }
                    dgv.Sort(dgv.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
                }
                catch
                {
                    MessageBox.Show("Data.000 not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                dgv.Rows.Clear();
                RappelzDataCipher.rappelz_data.Clear();
                string connect = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", OPT.GetString("db.server"), OPT.GetString("db.name"), OPT.GetString("db.account"), SFM.GetDecryptMD5(OPT.GetString("db.password")));
                SqlConnection sqlcon = new SqlConnection(connect);
                sqlcon.Open();
                pBar.Value = 0;
                SqlDataReader tbCount = new SqlCommand("SELECT COUNT(*) FROM data000_idx", sqlcon).ExecuteReader();
                tbCount.Read();
                pBar.Maximum = int.Parse(tbCount[0].ToString()) + 1;
                tbCount.Close();

                SqlDataReader drExecute = new SqlCommand("SELECT * FROM data000_idx", sqlcon).ExecuteReader();
                while (drExecute.Read())
                {
                    RAPPELZDATA_INDEX rappelz_index = new RAPPELZDATA_INDEX();
                    rappelz_index.name = drExecute["name"].ToString();
                    rappelz_index.hash = drExecute["hash"].ToString();
                    rappelz_index.size = Int32.Parse(drExecute["size"].ToString());
                    rappelz_index.DataID = Int32.Parse(drExecute["location"].ToString());
                    rappelz_index.offset = Int32.Parse(drExecute["offset"].ToString());
                    RappelzDataCipher.rappelz_data.Add(rappelz_index);
                    pBar.Value++;
                }

                file_count.Text = "File Count: " + RappelzDataCipher.rappelz_data.Count.ToString();

                dgv.RowCount = RappelzDataCipher.rappelz_data.Count + 1;
                for (int i = 0; i < RappelzDataCipher.rappelz_data.Count; i++)
                {
                    dgv.Rows[i].Cells[0].Value = RappelzDataCipher.rappelz_data[i].name;
                    dgv.Rows[i].Cells[1].Value = RappelzDataCipher.rappelz_data[i].hash;
                    dgv.Rows[i].Cells[2].Value = RappelzDataCipher.rappelz_data[i].size;
                    dgv.Rows[i].Cells[3].Value = RappelzDataCipher.rappelz_data[i].DataID;
                    dgv.Rows[i].Cells[4].Value = RappelzDataCipher.rappelz_data[i].offset;

                }
                dgv.Sort(dgv.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
                sqlcon.Close();
                sqlcon.Dispose();
            }
        }

        public static void Import_To_DB(ProgressBar pBar)
        {
            string connect = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", OPT.GetString("db.server"), OPT.GetString("db.name"), OPT.GetString("db.account"), SFM.GetDecryptMD5(OPT.GetString("db.password")));
            SqlConnection sqlcon = new SqlConnection(connect);
            sqlcon.Open();
            pBar.Value = 0;
            pBar.Maximum = RappelzDataCipher.rappelz_data.Count;
            new SqlCommand("TRUNCATE TABLE data000_idx", sqlcon).ExecuteNonQuery();
            foreach (RAPPELZDATA_INDEX rappelz_index in RappelzDataCipher.rappelz_data)
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[data000_idx] ([name],[hash],[size],[location],[offset]) VALUES (@pName, @pHash, @pSize, @pLocation, @pOffset)", sqlcon);
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@pName", SqlDbType.VarChar).Value = rappelz_index.name;
                cmd.Parameters.Add("@pHash", SqlDbType.VarChar).Value = rappelz_index.hash;
                cmd.Parameters.Add("@pSize", SqlDbType.BigInt).Value = rappelz_index.size;
                cmd.Parameters.Add("@pLocation", SqlDbType.TinyInt).Value = rappelz_index.DataID;
                cmd.Parameters.Add("@pOffset", SqlDbType.BigInt).Value = rappelz_index.offset;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                pBar.Value++;
            }
            sqlcon.Close();
            sqlcon.Dispose();
        }

        public static void Extension_Filter(TextBox ext_filter_box, Label file_count, ProgressBar pBar, DataGridView dgv)
        {
            if (ext_filter_box.Text.Length == 3)
            {
                if (dgv.Rows.Count > 0) { dgv.Rows.Clear(); }
                int row = 0;
                pBar.Value = 0;
                pBar.Maximum = RappelzDataCipher.rappelz_data.Count;

                foreach (RAPPELZDATA_INDEX rappelz_index in RappelzDataCipher.rappelz_data)
                {
                    if (rappelz_index.name.EndsWith("." + ext_filter_box.Text.ToLower()))
                    {
                        dgv.Rows.Add();
                        dgv.Rows[row].Cells[0].Value = rappelz_index.name;
                        dgv.Rows[row].Cells[1].Value = rappelz_index.hash;
                        dgv.Rows[row].Cells[2].Value = rappelz_index.size;
                        dgv.Rows[row].Cells[3].Value = rappelz_index.DataID;
                        dgv.Rows[row].Cells[4].Value = rappelz_index.offset;
                        row++;
                    }
                    else { }
                    pBar.Value++;
                }
                dgv.Sort(dgv.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
                file_count.Text = "File Count: " + dgv.Rows.Count;
            }
            else if (ext_filter_box.Text.Length == 0)
            {
                if (dgv.Rows.Count > 0) { dgv.Rows.Clear(); }
                int row = 0;
                pBar.Value = 0;
                pBar.Maximum = RappelzDataCipher.rappelz_data.Count;

                foreach (RAPPELZDATA_INDEX rappelz_index in RappelzDataCipher.rappelz_data)
                {
                    dgv.Rows.Add();
                    dgv.Rows[row].Cells[0].Value = rappelz_index.name;
                    dgv.Rows[row].Cells[1].Value = rappelz_index.hash;
                    dgv.Rows[row].Cells[2].Value = rappelz_index.size;
                    dgv.Rows[row].Cells[3].Value = rappelz_index.DataID;
                    dgv.Rows[row].Cells[4].Value = rappelz_index.offset;
                    row++;
                    pBar.Value++;
                }
                dgv.Sort(dgv.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
                file_count.Text = "File Count: " + dgv.Rows.Count;
            }
        }

        public static void Search(TextBox name_box, DataGridView dgv)
        {
            for (int i = 0; i < dgv.RowCount; i++)
            {
                try
                {
                    if (dgv.Rows[i].Cells[0].Value.ToString().Contains(name_box.Text))
                    {
                        dgv.FirstDisplayedScrollingRowIndex = i;
                        dgv.Rows[i].Selected = true;
                        break;
                    }
                    else { dgv.Rows[i].Selected = false; }
                }
                catch { }
            }
        }

        public static void Size_Changed(DataGridView dgv)
        {
            dgv.Columns[0].Width = int.Parse(Math.Round(dgv.Width / 3.372).ToString());
            dgv.Columns[1].Width = int.Parse(Math.Round(dgv.Width / 3.372).ToString());
            dgv.Columns[2].Width = int.Parse(Math.Round(dgv.Width / 8.43).ToString());
            dgv.Columns[3].Width = int.Parse(Math.Round(dgv.Width / 8.43).ToString());
            dgv.Columns[4].Width = int.Parse(Math.Round(dgv.Width / 8.43).ToString());
        }
    }
}
